//
//  SCNTextView.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/16.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNTextView: UITextView {

    var placeText: String? {
        didSet {
            placeL.text = placeText
            let textSize = placeText?.getTextRectSize(font: UIFont.systemFont(ofSize: 17.0), size: CGSize(width: SCNConstants.SCREENWIDTH - 40,height: 100)).size
            placeL.frame = CGRect(x: 8,y: 8, width: (textSize?.width)!,height: (textSize?.height)!)
        }
    }
    
    
    // 占位
    lazy var placeL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = .lightGray
        lab.font = self.font
        lab.numberOfLines = 0
        return lab
    }()
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    
        addSubview(placeL)
   
        NotificationCenter.default.addObserver(self, selector: #selector(textChanged), name: .UITextViewTextDidChange, object: self)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

extension SCNTextView {
    
    func textChanged() {
        
        placeL.isHidden = self.hasText
    }
}
